import 'dart:convert';

import 'package:client/common/fun.dart';
import 'package:flutter/material.dart';
import 'package:client/common/my_http.dart';

class ChangePwdRoute extends StatelessWidget {
  const ChangePwdRoute({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("修改密码"),
      ),
      body: _buildBody(context),
    );
  }

  _buildBody(context) {
    var oldPwdController = TextEditingController();
    var newPwdController = TextEditingController();
    var confirmPwdController = TextEditingController();
    var regView = Column(
      children: [
        TextField(
          autofocus: true,
          controller: oldPwdController,
          decoration: const InputDecoration(
              labelText: "密码",
              hintText: "密码",
              prefixIcon: Icon(Icons.border_all_sharp)),
        ),
        TextField(
          autofocus: true,
          controller: newPwdController,
          decoration: const InputDecoration(
              labelText: "新密码",
              hintText: "新密码",
              prefixIcon: Icon(Icons.password)),
        ),
        TextField(
          autofocus: true,
          controller: confirmPwdController,
          decoration: const InputDecoration(
              labelText: "重复新密码",
              hintText: "重复新密码",
              prefixIcon: Icon(Icons.password)),
        ),
        Container(
          padding: EdgeInsets.all(30),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text("取消")),
              TextButton(
                  onPressed: () async {
                    var oldPwd = oldPwdController.text;
                    var newPwd = newPwdController.text;
                    var confirmPwd = confirmPwdController.text;

                    if (oldPwd == "") {
                      Fun.dialog("错误", "密码不能为空");
                      return;
                    }
                    if (newPwd == "") {
                      Fun.dialog("错误", "新密码不能为空");
                      return;
                    }
                    if (newPwd != confirmPwd) {
                      Fun.dialog("错误", "两次密码不一致");
                      return;
                    }

                    var data = {
                      'old_pwd': oldPwd,
                      'new_pwd': newPwd,
                      'confirm_pwd': confirmPwd
                    };
                    MyHttp.changePwd(data);
                  },
                  child: Text("确定")),
            ],
          ),
        )
      ],
    );
    return regView;
  }
}
